/****************************************************************************/
/***                                                                      ***/
/***   (C) 2012-2014 Dierk Ohlerich et al., all rights reserved.          ***/
/***                                                                      ***/
/***   Released under BSD 2 clause license, see LICENSE.TXT               ***/
/***                                                                      ***/
/****************************************************************************/

mtrl BlurXShader 
{
  cbuffer cbc0 : cs(0) : register(0)
  {
    int2 TextureSize;
    float2 BlurRadius;
  };

  cs hlsl5
  {
    SamplerState sam0 : register(s0);
    Texture2D tex0 : register(t0);
    RWTexture2D<float4> target : register(u0); 

    [numthreads(256,1,1)]
    void main(uint3 tid:SV_DispatchThreadID)
    {
      float r = BlurRadius.y/2;
	  int m = int(r);
	  float a = r-m;
      float amp = 1/(2*r+1);
	  int i;

      float x = float(tid.x)/float(TextureSize.x);
      float yscale = 1.0/float(TextureSize.y);
	  float2 pixoffs = float2(x+0.5/TextureSize.x, 0.5/TextureSize.y);

	  float4 sum = tex0.SampleLevel(sam0,pixoffs,0);
	  for (i=0; i<m; i++)
	  {
		sum += tex0.SampleLevel(sam0,float2(0, (i+1)*yscale)+pixoffs,0);	
		sum += tex0.SampleLevel(sam0,float2(0, -(i+1)*yscale)+pixoffs,0);	
	  }
	  sum += a * tex0.SampleLevel(sam0,float2(0, (m+1)*yscale)+pixoffs,0);	
	  sum += a * tex0.SampleLevel(sam0,float2(0, -(m+1)*yscale)+pixoffs,0);	

	  for (i=0; i<TextureSize.y; i++)
	  {
		target[uint2(tid.x,i)] = sum*amp; 
		sum += tex0.SampleLevel(sam0,float2(0, (i+r+1)*yscale)+pixoffs,0);	
		sum -= tex0.SampleLevel(sam0,float2(0, (i-r)*yscale)+pixoffs,0);	
	  }
    }
  };
} 


mtrl BlurYShader 
{
  cbuffer cbc0 : cs(0) : register(0)
  {
    int2 TextureSize;
    float2 BlurRadius;
  };

  cs hlsl5
  {
    SamplerState sam0 : register(s0);
    Texture2D tex0 : register(t0);
    RWTexture2D<float4> target : register(u0); 

    [numthreads(1,256,1)]
    void main(uint3 tid:SV_DispatchThreadID)
    {
      float r = BlurRadius.x/2;
	  int m = int(r);
	  float a = r-m;
      float amp = 1/(2*r+1);
	  int i;

      float y = float(tid.y)/float(TextureSize.y);
      float xscale = 1.0/float(TextureSize.x);
	  float2 pixoffs = float2(0.5/TextureSize.x, y+0.5/TextureSize.y);

	  float4 sum = tex0.SampleLevel(sam0,pixoffs,0);
	  for (i=0; i<m; i++)
	  {
		sum += tex0.SampleLevel(sam0,float2((i+1)*xscale,0)+pixoffs,0);	
		sum += tex0.SampleLevel(sam0,float2(-(i+1)*xscale,0)+pixoffs,0);	
	  }
	  sum += a * tex0.SampleLevel(sam0,float2((m+1)*xscale,0)+pixoffs,0);	
	  sum += a * tex0.SampleLevel(sam0,float2(-(m+1)*xscale,0)+pixoffs,0);	

	  for (i=0; i<TextureSize.x; i++)
	  {
		target[uint2(i,tid.y)] = sum*amp; 
		sum += tex0.SampleLevel(sam0,float2((i+r+1)*xscale,0)+pixoffs,0);	
		sum -= tex0.SampleLevel(sam0,float2((i-r)*xscale,0)+pixoffs,0);	
	  }
    }
  };
} 

/****************************************************************************/
